// Versi�n 2.0 del robot CoachColab

package com.wavecoler.coach.bots;

import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.ElementType;
import com.google.wave.api.Gadget;
import com.google.wave.api.Participants;
import com.google.wave.api.Wavelet;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
import com.google.wave.api.event.GadgetStateChangedEvent;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CoachColabUsuarios extends AbstractRobot {
	
private static int contador = 1;
private static String PhaseSession = "";
private static int numParticipantes = 3;
private static final Logger log = Logger.getLogger(CoachColabUsuarios.class.getName());

//private String consumerToken = "wavecoler-sup@wavecoler.lag.itesm.mx";
//private String tokenSecret = "k7KK4f30Ik2w97eHTpXZRP8lb9YGsBKeydbjPWlq3qudmKLi";


private String consumerToken = "collabsup@wavecoler.lag.itesm.mx";
private String tokenSecret = "XAm2LdJlYX6rcszPhjShs2ALlDVQVDnhuojNM6Fymk89MG4s";

public CoachColabUsuarios() {

	// Registration in WIAB
	setupOAuth(consumerToken, tokenSecret,
	        "http://wavecoler.lag.itesm.mx:9898/robot/dataapi/rpc");
	setAllowUnsignedRequests(true);
}

public void logString(String string) {
	  log.info(string);
	}

	
@Override
 protected String getRobotName() {
 return "Google Wave Robot CoachColab Supervisor";
 }

@Override
 protected String getRobotAvatarUrl() {
    return "http://code.google.com/apis/wave/extensions/robots/images/robot_avatar.png";
 }

 @Override
 protected String getRobotProfilePageUrl() {
 return "http://wavecolersup.appspot.com";
 }

 /**
 @Override
 public void onBlipSubmitted(BlipSubmittedEvent event) {
 Blip blip = event.getBlip();
 blip.append("\n You typed: "+blip.getContent());
 }
 **/

 @Override
 public void onWaveletParticipantsChanged(WaveletParticipantsChangedEvent event) {
	 log.info("Entre a onWaveletParticipantsChanged");
 // event.getWavelet().reply("\n Saludos, soy el Robot CoachColab.");
     Participants p = event.getWavelet().getParticipants();
     log.info("Number of Participants is "+p.size());
 }

 @Override
 public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
	   log.info("Entre a WaveletSelfAdded");
       Wavelet wave = event.getWavelet();
       wave.getRootBlip().append("Coach de Colaboracion");
       Blip blip = wave.reply("\n Saludos, soy el Robot CoachColab. \n Agrega a un participante en la siguiente forma.");
 	   String gadgetUrl = "http://wavecoler.googlecode.com/svn/trunk/WaveColer/war/gadgets/agregausuario.xml";
	   Gadget gadget = new Gadget(gadgetUrl);
	   blip.append(gadget);    
       PhaseSession = "INIT";
       log.info("Phase = "+PhaseSession);
 }
 
 @Override
	public void onGadgetStateChanged(GadgetStateChangedEvent event) {
	   log.info("Entre a GadgetStateChanged");
		Wavelet wave = event.getWavelet();
		Blip blip = event.getBlip();
		Gadget gadget = Gadget.class.cast(blip.first(ElementType.GADGET).value());
		String usuario = gadget.getProperty("agregarContacto");
		log.info("Usuario = "+ usuario);
		Blip blip1 = blip;
		if (usuario!=null){
		    wave.getParticipants().add(usuario);
		    blip1 = wave.reply("\nBienvenido "+ usuario +"!");
		    contador++;
		    log.info("Total de Participantes = "+ wave.getParticipants().size()+ " Contador ="+contador);       
		}
		if (contador != numParticipantes){
			PhaseSession = "WAITING";
			log.info("WAITING");
			blip1.append(" Estamos por comenzar la sesi�n, vamos a esperar a los demas para iniciar.");

		}
		else {
			PhaseSession = "READY";
			log.info("READY");
			blip1.append(" Todos los participantes estón listos, vamos a iniciar la sesión colaborativa.");
			
			wave.getParticipants().add("wavecoler@wavecoler.lag.itesm.mx");
		}
		
	}
 
  
 public void makeDebugBlip(Wavelet wavelet, String text) {
	  wavelet.reply(text);
	}

	public void OnRobotAdded(WaveletSelfAddedEvent e) {
	  makeDebugBlip(e.getWavelet(),"Probando Robot");
	}

}